#!!/bin/env ruby
# encoding: utf-8

def str(len)
	ustr = 'a'
	len.times{|i| ustr+='a'}
	return ustr
end

def check_login_page
  goto 'http://mutualforce.7bits.it/homeAbout.html'

  a(id: "js-login", class: "b-quick-access__button_link", href: "loginDialog.html", text: "Login!").should be_present
  a(id: "js-login", class: "b-quick-access__button_link", href: "loginDialog.html", text: "Login!").click
  
  text_field(class: "b-sign-in__new_input_text", name: "j_username").should be_present
  text_field(class: "b-sign-in__new_input_password", name: "j_password").should be_present

  
  tableA = form(name: "login_form").table.tbody
  tableA[0][0].a(text: "Forgot password?").wait_until_present(5)
  tableA[1][0].a(text: "Sign Up").wait_until_present(5) 
  tableA[0][1].button(name: "submit", value: "Sign In", class: "b-button b-sign-in__new_input_submit").wait_until_present(5)

  ary = []
  hrefs = []
  ary = browser.links
  ary.each do |elem|
  	hrefs.push(elem.attribute_value("href"))  	
  end

  (hrefs.include? "https://mutualforce.7bits.it/socialTwitter.html?state=registry").should be_truthy
  (hrefs.include? "https://mutualforce.7bits.it/socialFacebook.html?state=registry").should be_truthy
  (hrefs.include? "https://mutualforce.7bits.it/socialLinkedin.html?state=registry").should be_truthy
end
  
def check_profile_page
  goto 'https://mutualforce.7bits.it/userHomePage.html'
  a(class: "b-button b-user-menu__link js-b-user-menu__login-link").click
  a(class: "b-user-menu__list-link", href: "userProfile.html").click  
  a(class: "b-tabs__list-item-link", href: "#tabs-profile-info").click
  (browser.text.include? "First Name:").should be_truthy
  text_field(id: "firstName", class: "b-form__input_text js-bad-form-input", name: "firstName").wait_until_present(3)
  (browser.text.include? "Last Name:").should be_truthy
  text_field(id: "lastName", class: "b-form__input_text js-bad-form-input", name: "lastName").wait_until_present(3)
  (browser.text.include? "Gender:").should be_truthy
  select_list(id: "gender",  class: "b-select b-form__select js-bad-form-input", name: "gender").wait_until_present(3)
  (browser.text.include? "Street address:").should be_truthy 
  text_field(id: "postAddressForm.street", class: "b-form__input_text js-bad-form-input", name: "postAddressForm.street").wait_until_present(3)
  (browser.text.include?  "City:").should be_truthy
  text_field(class: "b-form__input_text js-bad-form-input", name: "postAddressForm.city", id: "postAddressForm.city").wait_until_present(3)
  (browser.text.include?  "State:").should be_truthy
  select_list(class: "b-select b-form__select js-bad-form-input", name: "postAddressForm.stateId", id: "postAddressForm.stateId").wait_until_present(3)
  (browser.text.include?  "Zip:").should be_truthy
  text_field(class: "b-form__input_number js-bad-form-input", name: "postAddressForm.zip", id: "postAddressForm.zip").wait_until_present(3)
  button(value: "Submit", class: "b-button b-form__input_submit").wait_until_present(3)

  a(href: "#tabs-avatar", class: "b-tabs__list-item-link").click
  img(class: "b-form__img", alt: "").should be_present
  text_field(class: "b-form__input_text b-form__custom_input_text_file js-b-form__custom_input_text_file-avatar").wait_until_present(3)     
                    
  #file_field(class: "b-form__custom_input_file js-b-form__custom_input_file-avatar js-input_file", name: "newAvatar").wait_until_present(6)
  #button(value: "Submit", class: "b-button b-form__input_submit").wait_until_present(3)
  (browser.text.include? "Your current photo:").should be_truthy
  (browser.text.include? "Change photo:").should be_truthy     

  a(href: "#tabs-change-password", class: "b-tabs__list-item-link").click 
  #TODO check form
  a(href: "#tabs-profile-info", class: "b-tabs__list-item-link").click
end  

def check_userprofile_page
  check_profile_page()
  h2(text: "Summary").should be_present
  a(class: "b-button js-export-data-from-linkedin", text: "Import data from").should be_present
  (browser.text.include? "Professional Summary:").should be_truthy
  text_field(class: "b-form__textarea b-form__field_height_125 js-summary-field js-bad-form-input", name: "summary", id: "summary").should be_present
  (browser.text.include? "Professional Experience:").should be_truthy
  text_field(id: "experience", name: "experience").should be_present
  #TODO дописать
end

def role_fields(roleName)
    select_list(name: "selectedRole", id: "selectedRole").select roleName
    button(text: "Submit").should be_present
    (browser.text.include? "User Information:").should be_truthy
    (browser.text.include? "First Name").should be_truthy
    (browser.text.include? "Last Name:").should be_truthy
    (browser.text.include? "Email (Username):").should be_truthy
    (browser.text.include? "Authorization Information:").should be_truthy  
    text_field(class: "b-form__input_text", name: "firstName", id: "firstName").should be_present
    text_field(class: "b-form__input_text", name: "lastName", id: "lastName").should be_present
    text_field(class: "b-form__input_text", name: "name", id: "name").should be_present
    text_field(class: "b-form__input_text", name: "registerCode", id: "registerCode").should be_present
end

def check_signup_page
  (browser.text.include? "Role:").should be_truthy
  selectRole = select_list(name: "selectedRole", id: "selectedRole", class: "b-select b-form__select b-form__select-sign-up js-select-item")
  selectRole.should be_present
  
  role_fields('Employer')
  role_fields('Mentor')
  role_fields('Mentee')
     
  selectRole.select "Employer"
  (browser.text.include? "Organization Information:").should be_truthy
  (browser.text.include? "Organization name:").should be_truthy
  (browser.text.include? "Fax:").should be_truthy
  (browser.text.include? "Enter any 6-20 characters authorization code:").should be_truthy  
  text_field(class: "b-form__input_text", name: "companyName", id: "companyName").should be_present
  text_field(class: "b-form__input_number", name: "companyFax", id: "companyFax").should be_present  
end



# (browser.text.include? "").should be_truthy
  # (browser.text.include? "").should be_truthy